﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;
using FULLTUNNELCONTROLLib;
using System.Threading;
using GPCSC.Webs;


namespace GPCSC.Web.sjsbyes
{
    public partial class TZGGEdit : PageBase 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                F_post();
                if (Usual.f_querystringToString("command") != "" && (Usual.f_querystringToString("command") != null))
                {
                    this.newfileid.Value = Usual.f_querystringToString("newsid");
                    shownews(this.newfileid.Value);
                    if (Usual.f_querystringToString("command") == "view")//查看
                    {
                        Button1.Style.Add("display", "none");
                    }
                    else//修改
                    {

                    }
                }
                else
                {
                    this.newfileid.Value = Guid.NewGuid().ToString();
                }
            }
        }
        protected void F_post()
        {
            DataSet ds = new SqlReadXml().ReaderDataSet("sel_POST_all");
            if (ds.Tables.Count > 0)
            {
                DropDownList1.DataSource = ds.Tables[0];
                DropDownList1.DataBind();
            }

        }
        protected void shownews(string newsid) //展现
        {
            Hashtable htable = new Hashtable();
            htable.Add("newsid", newsid);
            DataSet ds = new DataReturn().ReturnData(htable, "sel_news_byid");
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    this.title.Value = ds.Tables[0].Rows[0]["NewsTitles"].ToString();
                    this.MtxtD_R_SD1_K.Value = ds.Tables[0].Rows[0]["FBdate"].ToString();
                    this.content1.Value = ds.Tables[0].Rows[0]["Newscontent"].ToString().Replace("^", "\n");
                    this.newsZZ.Value = ds.Tables[0].Rows[0]["NewsZZ"].ToString();
                    this.DropDownList1.SelectedValue = ds.Tables[0].Rows[0]["Postid"].ToString();
                    this.DropDownList1.Text = ds.Tables[0].Rows[0]["PostName"].ToString();
                }
            }
            buidfile();
        }
        protected void editnews()//修改
        {
            try
            {
                Model.newstable news = new BLL.newstable().GetModel(new Guid(this.newfileid.Value));
                news.NewsTitles = this.title.Value;//新闻标题
                if (this.MtxtD_R_SD1_K.Value == "")//新闻发布时间
                {
                    news.FBdate = DateTime.Now.Date.ToString();
                }
                else
                {
                    news.FBdate = this.MtxtD_R_SD1_K.Value;
                }


                news.Newscontent = this.content1.Value.Replace("\n", "^");//新闻内容
                news.NewsZZ = this.newsZZ.Value;
                news.IsDel = 0;
                news.IsFsDx = Usual.f_convertInt32(this.MtxtD_IsFsDx.Value);
                new BLL.newstable().Update(news);
               
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }

        protected void LCallBack_Callback(object sender, NickLee.Web.UI.CallBackEventArgs e)
        {
            try
            {
                if (e.Parameters[0].ToString() == "del")
                {
                    string fileid = e.Parameters[1].ToString();
                    Hashtable htable = new Hashtable();
                    htable.Add("@NewsfilesId", fileid);
                    new SqlReadXml().NotDoSQL("del_file_byid", htable);
                    buidfile();
                }
                else
                {
                    buidfile();
                }
            }
            catch (Exception ee)
            {
                throw ee;
            }
            finally
            {
                Panel1.RenderControl(e.Output);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string type = this.MtxtD_IsFsDx.Value;
            //if (type == "1")
            //{
            //    senddx();
            //}
            if (Usual.f_querystringToString("command") != "" && Usual.f_querystringToString("command") != null)
            {
                editnews();
            }
            else
            {
                saveNews();
            }
            Hashtable htab = new Hashtable();
            htab.Add("Postid", DropDownList1.SelectedItem.Value);
            htab.Add("PostName", DropDownList1.SelectedItem.Text);
            databyhtable dh = new databyhtable();
            dh.databyhtables(htab, "update", "newstable", "NewsID", this.newfileid.Value);
            this.Response.Write("<script>alert('操作成功！');window.location.href='TZGGList.aspx';</script>");

        }
        /// <summary>
        /// 保存新闻
        /// </summary>
        protected void saveNews()
        {
            try
            {
                Model.newstable news = new Model.newstable();
                news.NewsID = new Guid(this.newfileid.Value);//新闻ID
                news.NewsTitles = this.title.Value;//新闻标题
                if (this.MtxtD_R_SD1_K.Value == "")//新闻发布时间
                {
                    news.FBdate = DateTime.Now.Date.ToString();
                }
                else
                {
                    news.FBdate = this.MtxtD_R_SD1_K.Value;
                }

                news.BTSBLMName = "1";
                news.NewsZZ = this.newsZZ.Value;
                news.Newscontent = this.content1.Value.Replace("\n", "^");//新闻内容
                news.IsDel = 0;
                news.IsFsDx = Usual.f_convertInt32(this.MtxtD_IsFsDx.Value);
                new BLL.newstable().Add(news);
                //this.Response.Write("<script>alert('添加通知公告成功！');window.location.href='TZGGList.aspx';</script>");
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }


        /// <summary>
        /// 绑定新闻附件
        /// </summary>
        protected void buidfile()
        {
            try
            {

                Hashtable htable = new Hashtable();
                string file = this.newfileid.Value;
                htable.Add("@NewsId", file);
                DataSet ds = new DataReturn().ReturnData(htable, "sel_newsfiles_all");
                if (ds != null)
                {
                    Repeater1.DataSource = ds;
                    Repeater1.DataBind();
                }

            }
            catch (Exception ee)
            {
                throw ee;
            }
        }


        #region

        protected void senddx()
        {
            try
            {
                FullTunnelServiceControl ctr1 = new FullTunnelServiceControl();
                ctr1 = new FullTunnelServiceControl();

                //string strControl_type = "connect";
                string strName = "0517C00113043";
                string strPwd = "123abc456ABC";
                string strServerip = "218.94.58.243";//218.94.58.243
                string strServerport = "9005";

                object hr = ctr1.StartAndManageConnect_User(ref strName, ref strPwd, ref strServerip, ref strServerport);
                if (hr.ToString() == "0")
                {        //链接成功
                    try
                    {
                        Thread thread = new Thread(new ThreadStart(SendFromDbThread));
                        thread.Start();
                    }
                    catch (Exception ee)
                    {
                        Label1.Text = "创建发送线程失败";
                    }
                }
                else
                {
                    Label1.Text = "连接失败";
                }
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }
        private  void SendFromDbThread()
        {
            string strControl_type = "send";
            string strName = "用户名";               //用户名
            string strPwd = "密码";                  //密码
            string strServerip = "218.94.58.243";//218.94.58.243
            string strServerport = "9005";
            string strSendtype = "1";
            string strSourcecode = "12";        //必须得填 没有的话也必须填
            //string strDestcode = "15195380453";        //发送的号码
            NotHTML nhtml = new NotHTML();
            string strContent = nhtml.NoHTML(this.content1.InnerText); 
            string strComid = "1";            //必须得填 没有的话也必须填
            string strNeedreceipt = "1";
            string seqID;


             FullTunnelServiceControl ctr1 = new FullTunnelServiceControl();
            
             DataSet ds = new DataSet();
             Hashtable htable = new Hashtable();
             ds = new SqlReadXml().ReaderDataSet("sel_user_all", htable);

             if (ds != null)
             {
                 if (ds.Tables.Count > 0)
                 {
                     if (ds.Tables[0].Rows.Count > 0)
                     {
                         int length = ds.Tables[0].Rows.Count;
                         for (int i = 0; i < length; i++)
                         {

                             if (ds.Tables[0].Rows[i]["TEL"].ToString() != "" && ds.Tables[0].Rows[i]["TEL"].ToString() != null)
                             {
                                 string strDestcode = ds.Tables[0].Rows[i]["TEL"].ToString();
                                 object oOut = null;
                                 object hr = ctr1.SendBusinessPkg(ref strControl_type, ref strName, ref strPwd, ref  strSourcecode,
                                                                                 ref strDestcode, ref strServerip, ref  strServerport, ref strContent,

                                                                                 ref strSendtype, ref strComid, ref strNeedreceipt, out seqID);
                             }
                         }
                      
                     }
                 }
             }


            
                //if (hr.ToString() == "1")
            //{
                //textBox1.Text = "发送业务包成功" + "     此条短信的SequenceID是：" + seqID;    
                //MessageBox.Show(seqID.ToString());
               //Label1.Text="发送业务包成功";

                FullTunnelServiceControl ctr1s = new FullTunnelServiceControl();
                object hrs = ctr1s.CloseConnect();
            //}
            //else
            //{
                //textBox1.Text = "发送业务包失败"+hr.ToString();
                //MessageBox.Show("发送业务包fail");
            //}
        }
        #endregion
    }
}